from dotenv import load_dotenv
from openai import OpenAI


import time

load_dotenv()
#
client = OpenAI()

messages = client.beta.threads.messages.list(thread_id="thread_u1SgyJ3jJo7KVOsOsiWe3lSH")


for msg in messages:
    print(msg.content)


while True:
    messages = client.beta.threads.messages.list(thread_id="thread_u1SgyJ3jJo7KVOsOsiWe3lSH")
    try:
        messages.data[0].content[0].image_file
        time.sleep(5)
        print("图片创建完成")
        break
    except:
        print("正在创建图片。。。。。")
        time.sleep(10)


file_id = messages.data[0].content[0].image_file.file_id

print(file_id)

data = client.files.content(file_id)

data_bytes = data.read()
# print(data_bytes)
with open("数据分析.png", "wb") as file:
    file.write(data_bytes)
